package com.example.demo.database.mydbpool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


public class PoolMain {
	
	public static void main(String[] args) {
		PoolConn poll = new PoolConn();
		PoolMain main = new PoolMain();
		for (int i = 0; i < 50; i++) {
//			PoolMain.WorkThread w = main.new WorkThread("a",poll);
//			w.start();
//			Thread th = new Thread(new WorkRunnable(poll));
//			th.start();
			FutureTask<Integer> ft = new FutureTask(new WorkCallable(poll));
			Thread th = new Thread(ft);
			th.start();
			try {
				System.out.println(ft.get());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}

	 class WorkThread extends Thread {
		private PoolConn conn;
		public WorkThread(String name, PoolConn pool) {
	        super(name);
	        this.conn = pool;
	    }
		
		@Override
		public void run() {
			try {
				Connection conn2 = conn.getConn(-1);
				ResultSet executeQuery = conn2.createStatement().executeQuery("select count(1) from dfyz10_userinfo ");
				while (executeQuery.next()) {
					String string = executeQuery.getString(1);
					System.out.println(string);
				}
				conn.released(conn2);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
	 static class WorkRunnable implements Runnable{
			private PoolConn conn;
			public WorkRunnable(PoolConn pool) {
		        this.conn = pool;
		    }
			@Override
			public void run() {
				try {
					Connection conn2 = conn.getConn(100);
					ResultSet executeQuery = conn2.createStatement().executeQuery("select count(1) from dfyz10_userinfo ");
					while (executeQuery.next()) {
						String string = executeQuery.getString(1);
						System.out.println(string);
					}
					conn.released(conn2);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}
	 static class WorkCallable implements Callable<Integer>{
			private PoolConn conn;
			public WorkCallable(PoolConn pool) {
		        this.conn = pool;
		    }
			@Override
			public Integer call() throws Exception {
				try {
					Connection conn2 = conn.getConn(100);
					ResultSet executeQuery = conn2.createStatement().executeQuery("select count(1) from dfyz10_userinfo ");
					Integer in = null;
					while (executeQuery.next()) {
						in = executeQuery.getInt(1);
					}
					conn.released(conn2);
					return in;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return null;
			}
		}

}
